package edu.gatech.asthma.activity;

import edu.gatech.asthma.R;
import edu.gatech.asthma.database.DBAdapter;
import edu.gatech.asthma.database.ProfileVO;
import edu.gatech.asthma.ui_support.HelpMenuHelper;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Second GUI screen for profile creation in the parent interface
 * Allows user to input height, weight, and ethnicity
 */
public class CreateProfileParent2Activity extends Activity {

	DBAdapter database;
	int identifier = -1;
	Activity currentActivity = this; //necessary to reference from button listener
	
	@Override
    protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_patient_information2);
        
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
        	identifier = extras.getInt("ID");
        }
        database = new DBAdapter(this);
        database.open();
        
        //Customize instructions to include child's name
        TextView instructions = (TextView) findViewById(R.id.profile_instructions);
        instructions.setText(getString(R.string.profile_parent_instructions)+" "+database.getProfile(identifier).getName()
        		+" "+getString(R.string.profile_parent_instructions2));
        
    	final EditText profileWeight = (EditText)findViewById(R.id.profile_weight);
    	final Spinner profileWeightUnits = (Spinner)findViewById(R.id.profile_weight_units);
    	final EditText profileHeight = (EditText)findViewById(R.id.profile_height);
    	final Spinner profileEthnicity = (Spinner)findViewById(R.id.profile_ethnicity);
    	final Spinner profileHeightUnits = (Spinner)findViewById(R.id.profile_height_units);
    	//the three views that need to be hidden if Centimeters is selected
    	final TextView feetInches1 = (TextView) findViewById(R.id.profile_feet_shorthand);
    	final TextView feetInches2 = (TextView) findViewById(R.id.profile_inches_shorthand);
    	final EditText profileInches = (EditText) findViewById(R.id.profile_height_inches);
    	
    	//create a dialog that asks child to have parents fill out the next screens
    	AlertDialog.Builder builder = new AlertDialog.Builder(this);
    	builder.setMessage(R.string.profile_contacts)
    		.setTitle(R.string.profile_contacts_title);
    	builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                // User clicked OK button
            	Intent i = new Intent(CreateProfileParent2Activity.this, ShowContactsActivity.class);
            	i.putExtra("ID", identifier);
            	CreateProfileParent2Activity.this.startActivity(i);
            }
        });
    	builder.setNegativeButton(R.string.not_now, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
            	// User clicked NOT NOW button
            	Intent i = new Intent(CreateProfileParent2Activity.this, HomeScreenActivity.class);
            	i.putExtra("ID", identifier);
            	CreateProfileParent2Activity.this.startActivity(i);
            }
        });
    	final AlertDialog dialog = builder.create();
        
        Button nextPage = (Button)findViewById(R.id.profile_parent_next);           
        nextPage.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	//user did not enter a weight
            	if (profileWeight.getText().toString().equals("")) {
            		Toast toast = Toast.makeText(currentActivity, 
        					currentActivity.getResources().getString(R.string.profile_weight_blank), Toast.LENGTH_LONG);
        			toast.show();
            	}
            	//user did not enter a height
            	else if (profileHeight.getText().toString().equals("")) {
            		Toast toast = Toast.makeText(currentActivity, 
        					currentActivity.getResources().getString(R.string.profile_height_blank), Toast.LENGTH_LONG);
        			toast.show();
            	}
            	else { //user entered all fields
	                ProfileVO profile = database.getProfile(identifier);
	            	profile.setWeight(Integer.parseInt(profileWeight.getText().toString()));
	            	profile.setWeight_unit(profileWeightUnits.getSelectedItemPosition());
	            	profile.setEthnicity(profileEthnicity.getSelectedItemPosition());
	            	
	            	int height = Integer.parseInt(profileHeight.getText().toString());
	            	int heightUnits = profileHeightUnits.getSelectedItemPosition();
	            	
	            	//if height was given in feet inches, must convert to inches
	            	if (heightUnits == 0) { //If feet/inches
	            		int inches = 0;
	                	if (!profileInches.getText().toString().equals("")){	//if user input something in inches field
	                		inches = Integer.parseInt(profileInches.getText().toString());
	                	}
	            		height = height*12 + inches;
	            	}
	            	profile.setHeight(height);
	            	profile.setHeight_unit(heightUnits);
	            	
	            	database.updateProfile(identifier,profile.getName(), profile.getGender(), profile.getBirthday(), profile.getWeight(), 
	            			profile.getWeight_unit(), profile.getHeight(), profile.getHeight_unit(), profile.getEthnicity(),
	            			profile.getBgColor(), profile.getTintColor(), profile.getAccentColor(), profile.getPic(), profile.getBest());
	            	dialog.show();
            	}
            }
        });
        profileHeightUnits.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				if (position==1) { //if centimeters is selected
					feetInches1.setVisibility(View.GONE);
					feetInches2.setVisibility(View.GONE);
					profileInches.setVisibility(View.GONE);
				}
				else {
					feetInches1.setVisibility(View.VISIBLE);
					feetInches2.setVisibility(View.VISIBLE);
					profileInches.setVisibility(View.VISIBLE);
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {}
        	
        });
    }
	
	/**
	 * User clicked the help button on this screen
	 * @param v ImageView clicked
	 */
	public void helpButtonClicked(View v) {
		HelpMenuHelper.displayHelpMessage(this);
	}
}
